<?php

require_once 'classes/LightPressPlugin.php';

class HeaderAd extends LightPressPlugin {
    
    var $constructor_args = array(
        'ad_file'           =>'file containing the ads to display',
        'bg_color'          => 'Ad background color',
        'text_color'        => 'Text color',
        'link_color'        => 'Link color');
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'Rotate ads in the header';
    

    var $active = true;
    var $hooks = array('pre_render');
    var $ad_file = '/home/ludo/header_ads.html';
    var $bg_color = 'fffff2';
    var $text_color = '000000';
    var $link_color = '990000';
    var $_ads;

    function HeaderAd(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run) { // refactor using plugin options
            $ads = @file($this->ad_file);
            if (is_array($ads)) {
                foreach ($ads as $i=>$ad) {
                    $ad = trim($ad);
                    if (!empty($ad))
                        $this->_ads[] = $ad;
                }
            }
            if (!$ads || is_null($this->_ads)) {
                $this->active = false;
                $this->hide();
            }
        }
    }

    function run($hook, &$plugin_data) {
        $tpl =& $this->_frontend->tpl;
        $ad = rand(0, count($this->_ads) - 1);
        $tpl->setFile('plugin_headerad', 'plugins/header_ad.xml');
        $tpl->setVar(array(
            'plugin_header_ad_text'         => $this->_ads[$ad],
            'plugin_header_ad_bg_color'     => $this->bg_color,
            'plugin_header_ad_text_color'   => $this->text_color,
            'plugin_header_ad_link_color'   => $this->link_color));
        $tpl->parse('PLUGIN_HEADERAD', 'plugin_headerad');
    }

}

?>